package com.xb.cloud.common.model;

import java.io.PrintWriter;
import java.io.StringWriter;

import com.alibaba.fastjson.JSON;
import com.xb.cloud.common.util.DateUtils;

/**
 * 
 * @ClassName: ExceptionLoggerDetail
 * @Description:异常信息封装类，方便日志存储
 * @author: 鲜备科技
 * @date: 2017年12月26日 下午3:59:47
 * 
 * @Copyright: 2017 www.xianbeitech.com Inc. All rights reserved.
 *
 */
public class ExceptionLoggerDetail {
	// 格式为yyyy-MM-dd HH:mm:ss
	private String date;
	// 错误出现类名
	private String className;
	// 错误出现方法
	private String methodName;
	// 请求参数
	private Object[] args;
	// 异常类型
	private String exceptionType;
	// 异常详细信息
	private String exceptionDetail;
	// 存放方法请求信息以及其他信息
	public ExceptionLoggerDetail() {
		// TODO Auto-generated constructor stub
	}
	public ExceptionLoggerDetail(Throwable e, String className, String methodName, Object[] args) {
		this.date = DateUtils.dateToString(DateUtils.getDate(), DateUtils.patternD);
		this.className = className;
		this.methodName = methodName;
		this.args = args;
		this.exceptionType = e.getClass().getSimpleName();
		this.exceptionDetail = getExceptionDetail(e);
	}

	public ExceptionLoggerDetail(String className, String methodName) {
		this.date = DateUtils.dateToString(DateUtils.getDate(), DateUtils.patternD);
		this.className = className;
		this.methodName = methodName;
	}
	public Object[] getArgs() {
		return args;
	}

	public void setArgs(Object[] args) {
		this.args = args;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getExceptionDetail() {
		return exceptionDetail;
	}

	public void setExceptionDetail(String exceptionDetail) {
		this.exceptionDetail = exceptionDetail;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public String getExceptionType() {
		return exceptionType;
	}

	public void setExceptionType(String exceptionType) {
		this.exceptionType = exceptionType;
	}

	private String getExceptionDetail(Throwable e) {
		try {
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			return "\r\n" + sw.toString() + "\r\n";
		} catch (Exception e2) {
			return "bad getErrorInfoFromException";
		}
	}
}
